﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Parse;
using ParseControls;

namespace ParseOnlineAdmin
{
    [ValueConversion(typeof(object), typeof(string))]
    public class ParseObjectToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ParseObjectMask mask = value as ParseObjectMask;
            if (mask != null)
            {
                ParseObject obj = mask.Po;
                if (obj != null)
                {
                    try
                    {
                        return obj.Get<string>(parameter.ToString());
                    }
                    catch (KeyNotFoundException knfe)
                    {
                        return string.Empty;
                    }
                    catch (Exception ex)
                    {
                        return string.Empty;
                    }
                }
            }


            return string.Empty;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            ParseObjectMask po = ((ParseDataContext)((HostWindow)Application.Current.MainWindow).DataContext).SelectedItem as ParseObjectMask;
            if (po != null)
            {
                try
                {
                    string key = parameter.ToString();
                    po.Po[key] = value.ToString();

                    // In order to update each change in a textbox with this binding converter 
                    // so for this case... you need no save button ... ;-) 
                    ParseOne.InformationSending(new ParseCustomEventArgs("Save:Current:Parse:Object"));

                    return po;
                }
                catch (Exception ex)
                {

                }
            }

            return null;
        }
    }

    [ValueConversion(typeof(string), typeof(string))]
    public class StringSplitConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = value as string;

            if (string.IsNullOrEmpty(val))
            {
                return "...";
            }

            return val != null ? val.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[0].ToUpper() : value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    [ValueConversion(typeof(string), typeof(string))]
    public class ToUpperConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = value as string;
            return val != null ? val.ToUpper() : value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    [ValueConversion(typeof(string), typeof(string))]
    public class ToLowerConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = value as string;
            return val != null ? val.ToLower() : value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    [ValueConversion(typeof(string), typeof(System.Windows.Media.ImageSource))]
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter != null)
            {
                string para = parameter.ToString();

                if (para == "empty")
                    return string.Empty;
            }

            if (value == null)
            {
                return string.Empty;
            }

            if (!File.Exists(value.ToString()))
                return null;

            try
            {
                MemoryStream ms = new MemoryStream();
                BitmapImage image = new BitmapImage();

                System.Drawing.Bitmap bmp = System.Drawing.Icon.ExtractAssociatedIcon(value.ToString()).ToBitmap();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                image.BeginInit();
                ms.Seek(0, SeekOrigin.Begin);
                image.StreamSource = ms;
                image.EndInit();

                return image;
            }
            catch (ArgumentException ae)
            {

            }
            catch (Exception ex)
            {

            }

            return null;
        }
        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    [ValueConversion(typeof(object), typeof(Visibility))]
    public class StringVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;

            if (string.IsNullOrEmpty(value.ToString()))
                return Visibility.Collapsed;

            return Visibility.Visible;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility? v = value as Visibility?;
            return ((v.HasValue) || (v.Value == Visibility.Collapsed)) ? null : "";
        }
    }

    [ValueConversion(typeof(int), typeof(TimeSpan))]
    public class IntTimeSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, 0);

            if (value == null)
                return ts;

            int SliderValue = (int)value;
            ts = new TimeSpan(0, 0, 0, 0, SliderValue);
            return ts;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TimeSpan ts = (TimeSpan)value;
            if (ts == null)
                return null;

            return ts.TotalSeconds;
        }
    }

    [ValueConversion(typeof(Boolean), typeof(Visibility))]
    public class BooleanToHiddenVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility rv = Visibility.Visible;
            try
            {
                var x = bool.Parse(value.ToString());
                if (x)
                {
                    rv = Visibility.Visible;
                }
                else
                {
                    rv = Visibility.Collapsed;
                }
            }
            catch (Exception)
            {
            }
            return rv;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    [ValueConversion(typeof(IEnumerable<object>), typeof(System.Windows.Visibility))]
    public class IEnumerableVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IEnumerable<object> list = value as IEnumerable<object>;

            System.Windows.Visibility visis = ((int)list.Count() == 0)
                ? System.Windows.Visibility.Collapsed
                : System.Windows.Visibility.Visible;

            return visis;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility? v = value as Visibility?;
            int i = ((v.HasValue) || (v.Value == Visibility.Collapsed)) ? 0 : 1;
            return i;
        }
    }

    [ValueConversion(typeof(object), typeof(System.Windows.Visibility))]
    public class ObjectNullVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            System.Windows.Visibility visis = (value == null)
                ? System.Windows.Visibility.Collapsed
                : System.Windows.Visibility.Visible;

            return visis;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility? v = value as Visibility?;
            int i = ((v.HasValue) || (v.Value == Visibility.Collapsed)) ? 0 : 1;
            return i;
        }
    }

    [ValueConversion(typeof(int), typeof(System.Windows.Visibility))]
    public class CountVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int count;

            int.TryParse(value.ToString(), out count);

            System.Windows.Visibility visis = ((int)count == 0)
                ? System.Windows.Visibility.Collapsed
                : System.Windows.Visibility.Visible;

            return visis;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility? v = value as Visibility?;
            int i = ((v.HasValue) || (v.Value == Visibility.Collapsed)) ? 0 : 1;
            return i;
        }
    }
}
